//Determine whether an integer is a palindrome. An integer is a palindrome when 
//it reads the same backward as forward. 
//
// Follow up: Could you solve it without converting the integer to a string? 
//
// 
// Example 1: 
//
// 
//Input: x = 121
//Output: true
// 
//
// Example 2: 
//
// 
//Input: x = -121
//Output: false
//Explanation: From left to right, it reads -121. From right to left, it becomes
// 121-. Therefore it is not a palindrome.
// 
//
// Example 3: 
//
// 
//Input: x = 10
//Output: false
//Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
// 
//
// Example 4: 
//
// 
//Input: x = -101
//Output: false
// 
//
// 
// Constraints: 
//
// 
// -231 <= x <= 231 - 1 
// 
// Related Topics 数学 
// 👍 1314 👎 0


package editor.cn;
//Java：Palindrome Number
public class P9PalindromeNumber{
    public static void main(String[] args) {
        Solution solution = new P9PalindromeNumber().new Solution();
        // TO TEST
        System.out.println(solution.isPalindrome(1000110001));
        //1000000001 true
        //1000030001 false
        //1000021   false
        //1000110001 true
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public boolean isPalindrome(int x) {
        //题目要求，负数不是回文数
        if (x < 0) {
            return false;
        }
        //指向最高位
        int count = 0;
        int temp = x;
        //遍历找到最高次位
        while ((temp=temp/10) != 0) {
            count++;
        }
//        指向最低位
        temp = 0;
        //每次遍历，从效果上cut出最高位和最低位比较
        while (count>temp) {

            int tail = (int) (x % Math.pow(10, temp+1));
            int front = (int) (x - x % Math.pow(10, count));
            //cut出最低位
            int tailNum = (int) (tail / Math.pow(10, temp++));
            //cut最高位
            int frontNum = (int) (front / Math.pow(10, count--));
            if (tailNum != frontNum) {
                return false;
            }
            x = x - tail - front;
        }
        return true;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}